﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;

namespace SharePointPowerToys
{
    class CustomEditorPart : EditorPart
    {
        const string SectionEnd = "<div style='width:100%' class='ms-formline'></div>";

        TextLinkEditorSectionEditorPartControl xmlLinkTextBox;
        TextLinkEditorSectionEditorPartControl xslLinkTextBox;

        CheckBox xslClientTransformCheckBox;
        CheckBox debugModeCheckBox;

        string errorMessage = string.Empty;

        public CustomEditorPart(string webPartId)
        {
            ID = "XmlPowerViewer" + webPartId;
            Title = "XML Caching Web Part";
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            try
            {
                xmlLinkTextBox = new TextLinkEditorSectionEditorPartControl("XML", true, true);

                xslLinkTextBox = new TextLinkEditorSectionEditorPartControl("XSL", true, true);

                //Controls.Add(new LiteralControl("<TABLE border=\"0\" cellSpacing=\"0\" width=\"100%\"><TBODY>"));

                Controls.Add(xmlLinkTextBox);

                Controls.Add(xslLinkTextBox);

                xslClientTransformCheckBox = new CheckBox();
                xslClientTransformCheckBox.ID = String.Concat(ID, "XSLClientTransformCheckBox");
                xslClientTransformCheckBox.Text = "Transform XSL on Client";
                Controls.Add(xslClientTransformCheckBox);

                Controls.Add(new LiteralControl(SectionEnd));

                debugModeCheckBox = new CheckBox();
                debugModeCheckBox.ID = String.Concat(ID, "DebugModeCheckBox");
                debugModeCheckBox.Text = "Debug Mode";
                Controls.Add(debugModeCheckBox);

                Controls.Add(new LiteralControl(SectionEnd));
                //Controls.Add(new LiteralControl("</TBODY></TABLE>"));
            }
            catch (Exception ex)
            {
                Controls.Add(new LiteralControl(ex.Message));
            }
            this.ChildControlsCreated = true;
        }

        //protected override void RenderContents(HtmlTextWriter writer)
        //{
        //    if (!String.IsNullOrEmpty(errorMessage))
        //    {
        //        //writer.renderbegintag(htmltextwritertag.tr);
        //        //writer.renderbegintag(htmltextwritertag.td);
        //        writer.WriteBreak();
        //        Label errormessage = new Label();
        //        errormessage.Text = errorMessage;
        //        //errormessage.applystyle(this.zone.errorstyle);
        //        errormessage.RenderControl(writer);
        //        //writer.renderendtag();  // td
        //        //writer.renderendtag();  // tr
        //    }

        //    this.EnsureChildControls();
        //}

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            var webPart = WebPartToEdit as XmlPowerViewer;
            if (webPart != null)
            {
                try
                {
                    webPart.XML = xmlLinkTextBox.Text;
                    webPart.XMLLink = xmlLinkTextBox.Link;
                    webPart.XMLLinkUsername = xmlLinkTextBox.Username;
                    webPart.XMLLinkPassword = xmlLinkTextBox.Password;
                    webPart.CacheSourceSeconds = xmlLinkTextBox.CacheSeconds;
                    webPart.XMLCacheKey = xmlLinkTextBox.CacheKey;

                    webPart.XSL = xslLinkTextBox.Text;
                    webPart.XSLLink = xslLinkTextBox.Link;
                    webPart.XSLLinkUsername = xslLinkTextBox.Username;
                    webPart.XSLLinkPassword = xslLinkTextBox.Password;
                    webPart.XSLResultsCacheSeconds = xslLinkTextBox.CacheSeconds;
                    webPart.XSLResultsCacheKey = xslLinkTextBox.CacheKey;
                    webPart.XSLClientTransform = xslClientTransformCheckBox.Checked;

                    webPart.DebugMode = debugModeCheckBox.Checked;
                }
                catch (System.Exception exc)
                {
                    errorMessage = String.Concat("Error: ", exc.Message);
                    return false;
                }
            }
            else
            {
                return false;
            }
            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            var webPart = WebPartToEdit as XmlPowerViewer;
            if (webPart != null)
            {
                xmlLinkTextBox.Text = webPart.XML;
                xmlLinkTextBox.Link = webPart.XMLLink;
                xmlLinkTextBox.Username = webPart.XMLLinkUsername;
                xmlLinkTextBox.Password = webPart.XMLLinkPassword;
                xmlLinkTextBox.CacheSeconds  = webPart.CacheSourceSeconds;
                xmlLinkTextBox.CacheKey = webPart.XMLCacheKey;

                xslLinkTextBox.Text = webPart.XSL;
                xslLinkTextBox.Link = webPart.XSLLink;
                xslLinkTextBox.Username = webPart.XSLLinkUsername;
                xslLinkTextBox.Password = webPart.XSLLinkPassword;
                xslLinkTextBox.CacheSeconds = webPart.XSLResultsCacheSeconds;
                xslLinkTextBox.CacheKey = webPart.XSLResultsCacheKey;

                xslClientTransformCheckBox.Checked = webPart.XSLClientTransform;

                debugModeCheckBox.Checked = webPart.DebugMode;
            }
        }
    }
}
